<?php 
/**
 * Класс, который реализовует базовые методы менеджемна даних в таблице
 *
 */

class FRM_View_Ext_Window extends FRM_View_Ext_Abstract
{

	/**
	 * Enter description here...
	 *
	 * @var string
	 */
	protected $closeAction = 'hide';

	/**
	 * Enter description here...
	 *
	 * @var PhpExt_Layout_FitLayout
	 */
	protected $layout = 'anchor';

	/**
	 * Enter description here...
	 *
	 * @var string
	 */
	protected $bodyStyle = 'padding:0px';


	/**
	 * Enter description here...
	 *
	 * @var string
	 */
	protected $buttonAlign = 'right';

	/**
	 * Конструтор
	 *
	 */
	public function __construct(){
		/**
		 * Назначаем переменные по умолчанию
		 */
		$this->autoHeight = true;
		$this->width = 400;

		$this->layout = new PhpExt_Layout_FitLayout();
	}

	/**
	 * Enter description here...
	 *
	 * @param string $closeAction
	 */
	public function SetCloseAction($closeAction){
		$this->closeAction = $closeAction;
	}

	/**
	 * Enter description here...
	 *
	 * @param string $layout
	 */
	public function SetLayout($layout){
		$this->layout = $layout;
	}

	/**
	 * Enter description here...
	 *
	 * @param string $bodyStyle
	 */
	public function SetBodyStyle($bodyStyle){
		$this->bodyStyle = $bodyStyle;
	}

	/**
	 * Enter description here...
	 *
	 * @param string $buttonAlign
	 */
	public function SetButtonAlign($buttonAlign){
		$this->buttonAlign = $buttonAlign;
	}

	public function render(){

		$this->preRender();
		$window = new PhpExt_Window();
		$window	->setAutoDestroy(true)
		-> setTitle($this->title)
		-> setModal(true)
		//->setAutoScroll(true)
		//-> setHeight(600)
		-> setAutoHeight(false)
		-> setWidth($this->width)
		-> setCloseAction($this->closeAction)
		//-> setLayout(new PhpExt_Layout_FormLayout())
		-> setBodyStyle($this->bodyStyle)
		-> setButtonAlign($this->buttonAlign);
		$window ->attachListener("activate",new PhpExt_Listener(PhpExt_Javascript::inlineStm("function(p){fixHeight(p)}")));

		foreach ($this->items as $item)
		{
			$window ->AddItem($item);
		}

		$window->addButton(PhpExt_Button::createTextButton("Отправить",PhpExt_Javascript::variable("save")));
		$window->addButton(PhpExt_Button::createTextButton("Отмена",PhpExt_Javascript::inlineStm("function() { editWindow.hide(); var tFields = editForm.findByType('textfield'); tFields[0].remoteValid=true;tFields[1].remoteValid=true;tFields[2].remoteValid=true;}")));

		$saveHandler = new PhpExt_Handler();
		$saveHandler = PhpExt_Javascript::inlineStm("function() {
	            if(editForm.form.isValid()) {
	            	submit = true;
	            	try {
	            	if(uploader != undefined && uploader != null){
	            	  for(i=0; i<uploader.length; i++){
	            	  	if(uploader[i].suo.getStats().files_queued>0){
		            	  	uploader_started++;
	            	  		uploader[i].suo.startUpload();
	  	            		submit = false;
	            	  	}      	  	
	            	  }
	            	}}catch(e){}
	            if(submit == true){
	               editForm.form.submit( {
	                  waitMsg : 'Подождите...', url : '/".self::$module."/".self::$controller."/save', failure : function(form, action) {
	                     Ext.MessageBox.alert('Ошибка', action.result.msg); }
	                  , success : function(form, action) {
	                     Ext.MessageBox.alert('Информация', 'Данные успешно сохранены'); editWindow.hide(); store.load( {
	                        params : {
	                           start : 0, limit : 25}
	                        }
	                     ); }
	                  }
	               ); 
	             }else{
	              Ext.Msg.wait('Подождите...','');
	             }
	            }
	            else {
	               Ext.MessageBox.alert('Ошибка', 'Введите корректные данные'); }
	            }");
		// Ext.OnReady -----------------------
		PhpExt_Ext::addOnReady(
		PhpExt_Javascript::assignNew("save", $saveHandler)
		);

		// Ext.OnReady -----------------------
		PhpExt_Ext::addOnReady(
		$window->getJavascript(false,$this->varName),
		PhpExt_Javascript::inlineStm("function fixHeight(p) { editForm.doLayout(); var maxHeight = Ext.getBody().getHeight() - 40;
    if (p.el.getHeight() > maxHeight) {
        p.setHeight(maxHeight);
        p.setWidth(editForm.el.getWidth()+45);
        p.items.get(0).setHeight(maxHeight-69);
    	} 
        if(p.y<0){
        	p.setPosition(p.x,5);
        }}
        $this->varName.on('show',function () {
        	if (".self::$controller.".currentId > 0)
        	{
        		var uploaderButtons = ".$this->varName.".find('xtype','uploadButton');

        		for (var i=0;i<uploaderButtons.length;i++)
        		{
        			uploaderButtons[i].setVisible(true);
        			var items = uploaderButtons[i].store.data.items;
        			
        		}
        	}
        	
        });
        $this->varName.on('hide',function () {var tFields = ".$this->varName.".find('remoteValid',false); for (var i=0;i<tFields.length;i++){tFields[i].remoteValid = true;} 
		/*var uploaders = ".$this->varName.".find('xtype','swfUpload');
		for (var i=0;i<uploaders.length;i++)
		{
			var items = uploaders[i].store.data.items;
			for (var j=0;j<items.length;j++)
			{
				items.remove(items[j]);
			}
		}*/
		if (typeof(editForm)==='object')
		{
			editForm.form.reset();
			editForm.doLayout();
			var tabs = ".$this->varName.".findByType('tabpanel');
			for (var i=0;i<tabs.length;i++){
				tabs[i].setActiveTab(0);
			}
			var fieldsets = ".$this->varName.".findByType('fieldset');
        	for (var i=0;i<fieldsets.length;i++)
    		{
    			fieldsets[i].collapse();
    		}
		}
        });
        ")
        );
	}
}